package com.airforce.utils;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class ExcelUtil {


    private final static Logger log = LoggerFactory.getLogger(ExcelUtil.class);

    public static String readXls(String path) {
        String text = "";
        try {
            FileInputStream is = new FileInputStream(path);
            HSSFWorkbook excel = new HSSFWorkbook(is);
            //获取第一个sheet
            HSSFSheet sheet0 = excel.getSheetAt(0);
            for (Iterator rowIterator = sheet0.iterator(); rowIterator.hasNext(); ) {
                HSSFRow row = (HSSFRow) rowIterator.next();
                for (Iterator iterator = row.cellIterator(); iterator.hasNext(); ) {
                    HSSFCell cell = (HSSFCell) iterator.next();
                    //根据单元的的类型 读取相应的结果
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) text += cell.getStringCellValue() + "\t";
                    else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
                        text += cell.getNumericCellValue() + "\t";
                    else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) text += cell.getCellFormula() + "\t";
                }
                text += ",";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.warn(String.valueOf(e));
        }

        return text;
    }

    public static String readXlsx(String path) {
        String text = "";
        try {
            OPCPackage pkg = OPCPackage.open(path);
            XSSFWorkbook excel = new XSSFWorkbook(pkg);

            //获取第一个sheet
            XSSFSheet sheet0 = excel.getSheetAt(0);
            for (Iterator rowIterator = sheet0.iterator(); rowIterator.hasNext(); ) {
                XSSFRow row = (XSSFRow) rowIterator.next();
                for (Iterator iterator = row.cellIterator(); iterator.hasNext(); ) {
                    XSSFCell cell = (XSSFCell) iterator.next();
                    //根据单元的的类型 读取相应的结果
                    if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) text += cell.getStringCellValue();
                    else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) text += cell.getNumericCellValue();
                    else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) text += cell.getCellFormula();
                }
                text += ",";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.warn(String.valueOf(e));
        }

        return text;
    }


    public static void main(String args[]) {

        ArrayList List = new ArrayList();
        for( int i=0;i <10;i++ ) //给数组增加10个Int元素
            List.add(i);
//..程序做一些处理
        List.remove(5);//将第6个元素移除
        for( int i=0;i <3;i++ ) //再增加3个元素
            List.add(i+20);


        System.out.println(List);//返回ArrayList包含的数组
    }
}